#!/bin/sh 
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian 
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#               adopted for jboss3/4 by Guido Guenther <agx@sigxcpu.org>
#               with parts borrowed from tomcat4's startup script
#
# Version:	$Id: jboss3.init,v 1.9 2004/06/21 11:43:46 agx Exp $
NAME=jboss4
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
JBOSS_HOME=/usr/share/$NAME
DESC="JBoss"
DEFAULT=/etc/default/$NAME
JBOSS_PID=/var/run/$NAME/$NAME.pid
export JBOSS_PID
JBOSS_SHUTDOWN=30
JBOSS_OUT=/var/log/$NAME/$NAME.out

# Run JBoss as this user ID
JBOSS_USER=jboss

# Use debian configuration as default
JBOSS_SERVER=default

# include jboss defaults if available
if [ -f $DEFAULT ] ; then
	. $DEFAULT
fi

if [ -f /usr/local/certificate-services/environment ] ; then
	. /usr/local/certificate-services/environment
fi


if [ -z "$JBOSS_USER" -a ! -z "$1" ]; then
	echo "Not ${1}ing $DESC as configured (JBOSS_USER is empty in $DEFAULT)."
	exit 0
fi

if [ `id -u` -ne 0 ]; then
	echo "Not starting/stopping $DESC, you are not root."
	exit 10
fi

# export JAVA_OPTS if set
[ -n "$JAVA_OPTS" ] && export JAVA_OPTS

# use specific config if set, the debian config otherwise
[ -n "$JBOSS_SERVER" ] && STARTUP_OPTS="-c $JBOSS_SERVER"

# use JBOSS_OPTS if set
[ -n "$JBOSS_OPTS" ] && STARTUP_OPTS="$STARTUP_OPTS $JBOSS_OPTS"

JBOSS_START="$JBOSS_HOME/bin/run.sh"
JBOSS_STOP="$JBOSS_HOME/bin/shutdown.sh -S"

#do not overwrite serverlog during startup
move_server_log () {
        if [ -f ${JBOSS_HOME}/server/default/log/server.log ] ; then
                DATE=`date +"%F"`
                FILENAME="${JBOSS_HOME}/server/default/log/server.log.${DATE}.restart"
                i=0
                while [ -f $FILENAME ] ; do
                        i=`expr $i + 1`
                        FILENAME="${JBOSS_HOME}/server/default/log/server.log.${DATE}.restart.${i}"
                done
                mv ${JBOSS_HOME}/server/default/log/server.log ${FILENAME}
        fi
}

case "$1" in
  start)
	if [ -z "$JAVA_HOME" ]; then
		echo "Could not start $DESC because no Java Development Kit"
		echo "(JDK) was found. Please download and install JDK 1.4 or higher and set"
		echo "JAVA_HOME in $DEFAULT to the JDK's installation directory."
		exit 0
	fi

	checkjboss=`ps -ef | egrep "/usr/local/java/bin/java.*name=run.sh.*org.jboss.Main" | grep -v grep`

	echo "checkjboss: $checkjboss"
        if [ "x${checkjboss}" != "x" ] ; then
                echo "NOT starting jboss, it's is already running."
                exit 255
        fi

	if [ -f $JBOSS_PID ] ; then

		pidnoofrunsh=$(cat $JBOSS_PID)
       		checkrunsh=$(ps -ef | grep $pidnoofrunsh | grep -v grep)
	        if [ "x${checkrunsh}" != "x" ] ; then
       			echo "NOT starting jboss, it's is already running."
                	exit 255
		fi

	else
		checkrunsh=`ps -ef | egrep "/usr/local/jboss/bin/run.sh" | grep -v grep`
                if [ "x${checkrunsh}" != "x" ] ; then
                        echo "NOT starting jboss, it's is already running."
                        exit 255
                fi

        fi


	echo -n "Starting $DESC using Java from $JAVA_HOME: "
	# Bug: startup off "all" fails when this dir exists 
	# There might be other configs, too so we clean up a bit more
	rm -rf /var/cache/jboss4/*/jboss.web/*

	# Bug: jboss needs the cwd writeable
  	cd /var/cache/$NAME/tmp

	move_server_log

	su -p -s /bin/sh "$JBOSS_USER" -c "$JBOSS_START $STARTUP_OPTS &" 2>&1 > $JBOSS_OUT

        startret=$?
        if [ $startret -ne 0 ] ;then
                echo "Start of jboss failed!"
                exit 15 
        fi      
        i=0
        echo "Waiting for jboss server to start:"
        while [ "`grep \"Server.*JBoss.*Started\" $JBOSS_HOME/server/${JBOSS_SERVER}/log/server.log 2>/dev/null`x" = "x" ] ; do
                echo -n "."
                sleep 2 
                i=`expr $i + 1`
                if [ $i -gt 90 ] ; then
			echo "While starting jboss server it did not complete the startup in 180 secs, check your server.log manually ..."
                        continue
                fi
        done
        echo "Jboss server is up and running."

	;;
  stop)
	echo -n "Stopping $DESC: "

	checkjboss=`ps -ef | egrep "/usr/local/java/bin/java.*name=run.sh.*org.jboss.Main" | grep -v grep`

        if [ "x${checkjboss}" = "x" ] ; then
                echo "NOT stoping jboss, it's not running."
                exit 
        fi
	su -p -s /bin/sh $JBOSS_USER -c "$JBOSS_STOP" >> "$JBOSS_OUT" 2>&1
        stopret=$?
        if [ $stopret -ne 0 ] ;then
                echo "stop failed"
                exit 2
        fi
        i=0
        while [ "`grep \"Server.*Shutdown.complete\" $JBOSS_HOME/server/default/log/server.log 2>/dev/null`x" = "x" ] ; do
                echo -n "."
                sleep 2
                i=`expr $i + 1`
                if [ $i -gt 90 ] ; then
			echo "Jboss did not exit after 180 sec, you need to manually kill it"
                        continue
                fi
        done


	rm -f "$JBOSS_PID"
	echo "$NAME."
	
	;;
  restart|force-reload)
  	$0 stop
	sleep 1
	$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 4
	;;
esac

exit 0
